package com.chy.sprbootmanual.controller;


import com.chy.sprbootmanual.bean.User;
import com.chy.sprbootmanual.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class UserLoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/tologin",method = RequestMethod.POST)
    public  String userLogin(User user, Model model, HttpSession session){
        Boolean flag = loginService.login(user);
        if (flag){
            session.setAttribute("USER",user);
            return "redirect:/main";
        }else {
            model.addAttribute("msg","账号密码有误，请重新登录");
            return "redirect:/login";
        }
    }

    @RequestMapping(value = "/main")
    public String tomain() {
        log.warn("进入首页");
        return "index";
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String toLogin(HttpSession session) {
        session.invalidate();
        return "login";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

}
